package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.dto.SoldRecordAddNewDTO;
import com.ycxy.sh.transaction.dao.entity.SoldRecord;
import com.ycxy.sh.transaction.dao.vo.SoldRecordStandardVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.CommoditySoldrecordMapper;
import com.ycxy.sh.transaction.service.CommoditySoldrecordService;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品售出记录
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Service
@Transactional
public class CommoditySoldrecordServiceImpl implements CommoditySoldrecordService {
    @Autowired
    private CommoditySoldrecordMapper commoditySoldrecordMapper;

    /**插入售出记录*/
    public void addNewSoldRecord(SoldRecordAddNewDTO soldRecordAddNewDTO){
        log.debug("开始处理添加售出记录的业务，参数：{}", soldRecordAddNewDTO);
        // 从参数对象中获取相册名称
        String commodityName = soldRecordAddNewDTO.getCommodityName();
        // 检查相册名称是否已经被占用（相册表中是否已经存在此名称的数据）
        log.debug("检查商品名称是否已经被占用");
        int count = commoditySoldrecordMapper.countByName(commodityName);
        if (count > 0) {
            // 是：售出记录存在该商品，添加记录失败
            String message = "添加记录失败，售出记录存在该商品！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 否：售出记录没有被占用，则向记录表中插入数据
        log.debug("商品名称没有被占用，将向售出记录表中插入数据");
        SoldRecord soldRecord = new SoldRecord();
        BeanUtils.copyProperties(soldRecordAddNewDTO, soldRecord);
        log.debug("即将插入商品售卖记录数据：{}", soldRecord);
        int row = commoditySoldrecordMapper.insert(soldRecord);
        if(row!=1){
            String message="添加售卖记录失败,服务器繁忙,请稍后在重试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("插入商品售卖记录数据完成");
    }
    /**删除售出记录*/
    public void deleteSoldRecord(Long id){
        //查询售出记录是否存在
        SoldRecordStandardVo standardById = commoditySoldrecordMapper.getStandardById(id);
        if(standardById==null){
            String message="删除售出记录失败,查询的售出记录不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }else {
            int num = commoditySoldrecordMapper.deleteById(id);
            log.debug("成功删除{}条数据！",num);
            if (num == 1) {
                String message1 = "售出记录删除成功";
                log.debug(message1);
                throw new ServiceException(ServiceCode.OK, message1);
            }
            String message2 = "售出记录删除失败";
            log.debug(message2);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message2);
        }
    }
    /**分页展示个人的售出记录*/
    public List<SoldRecord> pageSoldrecord(Integer page, Integer count,Long userid){
        return commoditySoldrecordMapper.pageById(page,count,userid);
    }
    /**查看售出记录总数*/
    public Integer selectSoldCount(Long userid){
        return commoditySoldrecordMapper.count(userid);
    }
}
